import Vue from 'vue'
import axios from 'axios'
import router from '@/router'
import store from '@/store'
import currentUser from '@/global/currentUser'

import qs from 'qs'
import merge from 'lodash/merge'
import { clearUserLoginInfo } from '@/utils'

const http1 = axios.create({
  timeout: 1000 * 30,
  withCredentials: true,
  headers: {
    'Content-Type': 'application/json; charset=utf-8'
  }
})

/**
 * 请求拦截
 */
http1.interceptors.request.use(config => {
  console.log("http1拦截请求", currentUser.user.uid, currentUser.user.nickName)
  config.headers['token'] = localStorage.getItem('user_token_'+currentUser.user.uid) // 请求头带上token
  return config
}, error => {
  return Promise.reject(error)
})

/**
 * 响应拦截
 */
http1.interceptors.response.use(response => {
  if (response.data && response.data.code === 10010) { // 10010, token失效
    // clearUserLoginInfo(this.$root.user.uid);
    router.push({ name: 'userlogin' })
  }
  return response
}, error => {
  return Promise.reject(error)
})


export default http1